package br.edu.curso.java.refatorar;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class AlunoDAO extends PessoaFisicaDAO<AlunoVO> {
    
	protected AlunoVO carregarObjeto(ResultSet rs) {

		AlunoVO  obj = new AlunoVO();
		
		try {
			obj = super.carregarObjeto(obj, rs);
			
			obj.setMatricula(rs.getInt("matricula"));
			obj.setDtMatricula(new Date(rs.getDate("dtMatricula").getTime()));
									
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return obj;
		
	}

	@Override
	protected int carregarCampos(PreparedStatement pstm, AlunoVO obj, int i) {

		try {
			
			i = super.carregarCampos(pstm, obj, i);
			
                        pstm.setInt(++i, obj.getMatricula());
			pstm.setDate(++i, new java.sql.Date( obj.getDtMatricula().getTime()));
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}


	@Override
	public String getNomeTabela() {
		// TODO Auto-generated method stub
		return "aluno";
	}

	@Override
	protected AlunoVO getObjeto() {
		// TODO Auto-generated method stub
		return new AlunoVO();
	}
        

    public  List<ProfessorVO> listarProfessoresDoAluno(Integer codigoAluno) {

    	ProfessorDAO professorDAO = new ProfessorDAO();
    	
    	List<ProfessorVO> lista = new ArrayList<ProfessorVO>();

		try {
			Connection conexao = getConexao();
			Statement stm = conexao.createStatement();
			
		                    PreparedStatement pstm =  conexao.prepareCall("select codigo from professor "
						   	+ "inner join alunosdoprofessor "
						   	+ "on professor.codigo = alunosdoprofessor.codigo_professor "
						   	+ "where alunosdoprofessor.codigo_aluno = ?;");
		                            
		                    pstm.setInt(1,  codigoAluno);
		
		                    ResultSet rs = pstm.executeQuery();
		
		                    while (rs.next()) {
				lista.add(professorDAO.retornar(rs.getInt(1)));
			}
			
			stm.close();
			conexao.close();
							
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return lista;
}   



}
